package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 存款界面
 */
public class SavePanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon sureImg = new ImageIcon("images/buttonSure.jpg");
    private JButton sureBtn = new JButton(sureImg);
    private JLabel sumLabel = new JLabel("存款金额:");
    private JTextField sumField = new JTextField();
    private Font font = new Font("黑体", Font.BOLD, 30);

    public SavePanel(ATMFrame atmFrame) {
        this.atmFrame =  atmFrame;
        this.setLayout(null);
        sumLabel.setBounds(300,200,240,50);
        sumLabel.setFont(font);
        sumField.setBounds(450,200,250,50);
        sumField.setFont(font);
        sureBtn.setBounds(884,240,140,50);
        this.add(sumLabel);
        this.add(sumField);
        this.add(sureBtn);
       sureBtn.addActionListener(this);
        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == sureBtn){
            User user = atmFrame.getLoginUser();
            try {
                user.setBlance(user.getBlance() + Double.parseDouble(sumField.getText()));
            }catch (NumberFormatException i){
                JOptionPane.showMessageDialog(this,"格式输入错误");
                return;
            }
            atmFrame.switchPanel("menu");
        }
    }
}
